#region Microsoft Public License
/*
 * (c) Copyright Chenjun Wu. http://utablesdk.codeplex.com
 * 
 * This source is subject to the Microsoft Public License (Ms-PL).
 * Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
 * All other rights reserved.
 */
#endregion

﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UTable.Input.Keyboard;
using UTable.Input;
using System.Windows;

namespace UTable.Objects.Controls
{
    public class UKeyboard: UControl, IKeyboard
    {
        static UKeyboard()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(UKeyboard), new FrameworkPropertyMetadata(typeof(UKeyboard)));
        }

        #region Private Fields

        private ModifierKeys _modifiers = ModifierKeys.None;

        private Dictionary<Key, UButton> _keyMap = new Dictionary<Key, UButton>();

        private Dictionary<Key, KeyStates> _keySatesMap = new Dictionary<Key, KeyStates>();

        private Dictionary<UButton, Key> _buttonMap = new Dictionary<UButton, Key>();

        private IInputNode _target = null;

        #endregion

        #region Cmd Buttons
        public UButton CmdTlide;
        public UButton Cmd1;
        public UButton Cmd2;
        public UButton Cmd3;
        public UButton Cmd4;
        public UButton Cmd5;
        public UButton Cmd6;
        public UButton Cmd7;
        public UButton Cmd8;
        public UButton Cmd9;
        public UButton Cmd0;
        public UButton CmdMinus;
        public UButton CmdPlus;
        public UButton CmdBackspace;


        public UButton CmdTab;
        public UButton CmdQ;
        public UButton CmdW;
        public UButton CmdE;
        public UButton CmdR;
        public UButton CmdT;
        public UButton CmdY;
        public UButton CmdU;
        public UButton CmdI;
        public UButton CmdO;
        public UButton CmdP;
        public UButton CmdOpenCrulyBrace;
        public UButton CmdEndCrultBrace;
        public UButton CmdOR;

        public UButton CmdCapsLock;
        public UButton CmdA;
        public UButton CmdS;
        public UButton CmdD;
        public UButton CmdF;
        public UButton CmdG;
        public UButton CmdH;
        public UButton CmdJ;
        public UButton CmdK;
        public UButton CmdL;
        public UButton CmdColon;
        public UButton CmdDoubleInvertedComma;
        public UButton CmdEnter;

        public UButton CmdShiftLeft;
        public UButton CmdShiftRight;
        public UButton CmdZ;
        public UButton CmdX;
        public UButton CmdC;
        public UButton CmdV;
        public UButton CmdB;
        public UButton CmdN;
        public UButton CmdM;
        public UButton CmdGreaterThan;
        public UButton CmdLessThan;
        public UButton CmdQuestion;

        public UButton CmdSpaceBar;
        public UButton CmdClear;
        public UButton CmdClose;
        #endregion

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            CmdTlide = this.GetTemplateChild("Button_CmdTlide") as UButton;
            Cmd1 = this.GetTemplateChild("Button_Cmd1") as UButton;
            Cmd2 = this.GetTemplateChild("Button_Cmd2") as UButton;
            Cmd3 = this.GetTemplateChild("Button_Cmd3") as UButton;
            Cmd4 = this.GetTemplateChild("Button_Cmd4") as UButton;
            Cmd5 = this.GetTemplateChild("Button_Cmd5") as UButton;
            Cmd6 = this.GetTemplateChild("Button_Cmd6") as UButton;
            Cmd7 = this.GetTemplateChild("Button_Cmd7") as UButton;
            Cmd8 = this.GetTemplateChild("Button_Cmd8") as UButton;
            Cmd9 = this.GetTemplateChild("Button_Cmd9") as UButton;
            Cmd0 = this.GetTemplateChild("Button_Cmd0") as UButton;
            CmdMinus = this.GetTemplateChild("Button_CmdMinus") as UButton;
            CmdPlus = this.GetTemplateChild("Button_CmdPlus") as UButton;
            CmdBackspace = this.GetTemplateChild("Button_CmdBackspace") as UButton;


            CmdTab = this.GetTemplateChild("Button_CmdTab") as UButton;
            CmdQ = this.GetTemplateChild("Button_CmdQ") as UButton;
            CmdW = this.GetTemplateChild("Button_CmdW") as UButton;
            CmdE = this.GetTemplateChild("Button_CmdE") as UButton;
            CmdR = this.GetTemplateChild("Button_CmdR") as UButton;
            CmdT = this.GetTemplateChild("Button_CmdT") as UButton;
            CmdY = this.GetTemplateChild("Button_CmdY") as UButton;
            CmdU = this.GetTemplateChild("Button_CmdU") as UButton;
            CmdI = this.GetTemplateChild("Button_CmdI") as UButton;
            CmdO = this.GetTemplateChild("Button_CmdO") as UButton;
            CmdP = this.GetTemplateChild("Button_CmdP") as UButton;
            CmdOpenCrulyBrace = this.GetTemplateChild("Button_CmdOpenCrulyBrace") as UButton;
            CmdEndCrultBrace = this.GetTemplateChild("Button_CmdEndCrultBrace") as UButton;
            CmdOR = this.GetTemplateChild("Button_CmdOR") as UButton;

            CmdCapsLock = this.GetTemplateChild("Button_CmdCapsLock") as UButton;
            CmdA = this.GetTemplateChild("Button_CmdA") as UButton;
            CmdS = this.GetTemplateChild("Button_CmdS") as UButton;
            CmdD = this.GetTemplateChild("Button_CmdD") as UButton;
            CmdF = this.GetTemplateChild("Button_CmdF") as UButton;
            CmdG = this.GetTemplateChild("Button_CmdG") as UButton;
            CmdH = this.GetTemplateChild("Button_CmdH") as UButton;
            CmdJ = this.GetTemplateChild("Button_CmdJ") as UButton;
            CmdK = this.GetTemplateChild("Button_CmdK") as UButton;
            CmdL = this.GetTemplateChild("Button_CmdL") as UButton;
            CmdColon = this.GetTemplateChild("Button_CmdColon") as UButton;
            CmdDoubleInvertedComma = this.GetTemplateChild("Button_CmdDoubleInvertedComma") as UButton;
            CmdEnter = this.GetTemplateChild("Button_CmdEnter") as UButton;

            CmdShiftLeft = this.GetTemplateChild("Button_CmdShift_Left") as UButton;
            CmdShiftRight = this.GetTemplateChild("Button_CmdShift_Right") as UButton;
            CmdZ = this.GetTemplateChild("Button_CmdZ") as UButton;
            CmdX = this.GetTemplateChild("Button_CmdX") as UButton;
            CmdC = this.GetTemplateChild("Button_CmdC") as UButton;
            CmdV = this.GetTemplateChild("Button_CmdV") as UButton;
            CmdB = this.GetTemplateChild("Button_CmdB") as UButton;
            CmdN = this.GetTemplateChild("Button_CmdN") as UButton;
            CmdM = this.GetTemplateChild("Button_CmdM") as UButton;
            CmdGreaterThan = this.GetTemplateChild("Button_CmdGreaterThan") as UButton;
            CmdLessThan = this.GetTemplateChild("Button_CmdLessThan") as UButton;
            CmdQuestion = this.GetTemplateChild("Button_CmdQuestion") as UButton;

            CmdSpaceBar = this.GetTemplateChild("Button_CmdSpaceBar") as UButton;
            CmdClear = this.GetTemplateChild("Button_CmdClear") as UButton;
            CmdClose = this.GetTemplateChild("Button_CmdClose") as UButton;

            CmdTlide.Pressed += OnButtonPressed;
            Cmd1.Pressed += OnButtonPressed;
            Cmd2.Pressed += OnButtonPressed;
            Cmd3.Pressed += OnButtonPressed;
            Cmd4.Pressed += OnButtonPressed;
            Cmd5.Pressed += OnButtonPressed;
            Cmd6.Pressed += OnButtonPressed;
            Cmd7.Pressed += OnButtonPressed;
            Cmd8.Pressed += OnButtonPressed;
            Cmd9.Pressed += OnButtonPressed;
            Cmd0.Pressed += OnButtonPressed;
            CmdMinus.Pressed += OnButtonPressed;
            CmdPlus.Pressed += OnButtonPressed;
            CmdBackspace.Pressed += OnButtonPressed;


            CmdTab.Pressed += OnButtonPressed;
            CmdQ.Pressed += OnButtonPressed;
            CmdW.Pressed += OnButtonPressed;
            CmdE.Pressed += OnButtonPressed;
            CmdR.Pressed += OnButtonPressed;
            CmdT.Pressed += OnButtonPressed;
            CmdY.Pressed += OnButtonPressed;
            CmdU.Pressed += OnButtonPressed;
            CmdI.Pressed += OnButtonPressed;
            CmdO.Pressed += OnButtonPressed;
            CmdP.Pressed += OnButtonPressed;
            CmdOpenCrulyBrace.Pressed += OnButtonPressed;
            CmdEndCrultBrace.Pressed += OnButtonPressed;
            CmdOR.Pressed += OnButtonPressed;

            CmdCapsLock.Pressed += OnButtonPressed;
            CmdA.Pressed += OnButtonPressed;
            CmdS.Pressed += OnButtonPressed;
            CmdD.Pressed += OnButtonPressed;
            CmdF.Pressed += OnButtonPressed;
            CmdG.Pressed += OnButtonPressed;
            CmdH.Pressed += OnButtonPressed;
            CmdJ.Pressed += OnButtonPressed;
            CmdK.Pressed += OnButtonPressed;
            CmdL.Pressed += OnButtonPressed;
            CmdColon.Pressed += OnButtonPressed;
            CmdDoubleInvertedComma.Pressed += OnButtonPressed;
            CmdEnter.Pressed += OnButtonPressed;

            CmdShiftLeft.Pressed += OnButtonPressed;
            CmdShiftRight.Pressed += OnButtonPressed;
            CmdZ.Pressed += OnButtonPressed;
            CmdX.Pressed += OnButtonPressed;
            CmdC.Pressed += OnButtonPressed;
            CmdV.Pressed += OnButtonPressed;
            CmdB.Pressed += OnButtonPressed;
            CmdN.Pressed += OnButtonPressed;
            CmdM.Pressed += OnButtonPressed;
            CmdGreaterThan.Pressed += OnButtonPressed;
            CmdLessThan.Pressed += OnButtonPressed;
            CmdQuestion.Pressed += OnButtonPressed;

            CmdSpaceBar.Pressed += OnButtonPressed;
            CmdClear.Pressed += OnButtonPressed;
            CmdClose.Pressed += OnButtonPressed;

            CmdTlide.Down += OnButtonDown;
            Cmd1.Down += OnButtonDown;
            Cmd2.Down += OnButtonDown;
            Cmd3.Down += OnButtonDown;
            Cmd4.Down += OnButtonDown;
            Cmd5.Down += OnButtonDown;
            Cmd6.Down += OnButtonDown;
            Cmd7.Down += OnButtonDown;
            Cmd8.Down += OnButtonDown;
            Cmd9.Down += OnButtonDown;
            Cmd0.Down += OnButtonDown;
            CmdMinus.Down += OnButtonDown;
            CmdPlus.Down += OnButtonDown;
            CmdBackspace.Down += OnButtonDown;


            CmdTab.Down += OnButtonDown;
            CmdQ.Down += OnButtonDown;
            CmdW.Down += OnButtonDown;
            CmdE.Down += OnButtonDown;
            CmdR.Down += OnButtonDown;
            CmdT.Down += OnButtonDown;
            CmdY.Down += OnButtonDown;
            CmdU.Down += OnButtonDown;
            CmdI.Down += OnButtonDown;
            CmdO.Down += OnButtonDown;
            CmdP.Down += OnButtonDown;
            CmdOpenCrulyBrace.Down += OnButtonDown;
            CmdEndCrultBrace.Down += OnButtonDown;
            CmdOR.Down += OnButtonDown;

            CmdCapsLock.Down += OnButtonDown;
            CmdA.Down += OnButtonDown;
            CmdS.Down += OnButtonDown;
            CmdD.Down += OnButtonDown;
            CmdF.Down += OnButtonDown;
            CmdG.Down += OnButtonDown;
            CmdH.Down += OnButtonDown;
            CmdJ.Down += OnButtonDown;
            CmdK.Down += OnButtonDown;
            CmdL.Down += OnButtonDown;
            CmdColon.Down += OnButtonDown;
            CmdDoubleInvertedComma.Down += OnButtonDown;
            CmdEnter.Down += OnButtonDown;

            CmdShiftLeft.Down += OnButtonDown;
            CmdShiftRight.Down += OnButtonDown;
            CmdZ.Down += OnButtonDown;
            CmdX.Down += OnButtonDown;
            CmdC.Down += OnButtonDown;
            CmdV.Down += OnButtonDown;
            CmdB.Down += OnButtonDown;
            CmdN.Down += OnButtonDown;
            CmdM.Down += OnButtonDown;
            CmdGreaterThan.Down += OnButtonDown;
            CmdLessThan.Down += OnButtonDown;
            CmdQuestion.Down += OnButtonDown;

            CmdSpaceBar.Down += OnButtonDown;
            CmdClear.Down += OnButtonDown;
            CmdClose.Down += OnButtonDown;

            CmdTlide.Up += OnButtonUp;
            Cmd1.Up += OnButtonUp;
            Cmd2.Up += OnButtonUp;
            Cmd3.Up += OnButtonUp;
            Cmd4.Up += OnButtonUp;
            Cmd5.Up += OnButtonUp;
            Cmd6.Up += OnButtonUp;
            Cmd7.Up += OnButtonUp;
            Cmd8.Up += OnButtonUp;
            Cmd9.Up += OnButtonUp;
            Cmd0.Up += OnButtonUp;
            CmdMinus.Up += OnButtonUp;
            CmdPlus.Up += OnButtonUp;
            CmdBackspace.Up += OnButtonUp;


            CmdTab.Up += OnButtonUp;
            CmdQ.Up += OnButtonUp;
            CmdW.Up += OnButtonUp;
            CmdE.Up += OnButtonUp;
            CmdR.Up += OnButtonUp;
            CmdT.Up += OnButtonUp;
            CmdY.Up += OnButtonUp;
            CmdU.Up += OnButtonUp;
            CmdI.Up += OnButtonUp;
            CmdO.Up += OnButtonUp;
            CmdP.Up += OnButtonUp;
            CmdOpenCrulyBrace.Up += OnButtonUp;
            CmdEndCrultBrace.Up += OnButtonUp;
            CmdOR.Up += OnButtonUp;

            CmdCapsLock.Up += OnButtonUp;
            CmdA.Up += OnButtonUp;
            CmdS.Up += OnButtonUp;
            CmdD.Up += OnButtonUp;
            CmdF.Up += OnButtonUp;
            CmdG.Up += OnButtonUp;
            CmdH.Up += OnButtonUp;
            CmdJ.Up += OnButtonUp;
            CmdK.Up += OnButtonUp;
            CmdL.Up += OnButtonUp;
            CmdColon.Up += OnButtonUp;
            CmdDoubleInvertedComma.Up += OnButtonUp;
            CmdEnter.Up += OnButtonUp;

            CmdShiftLeft.Up += OnButtonUp;
            CmdShiftRight.Up += OnButtonUp;
            CmdZ.Up += OnButtonUp;
            CmdX.Up += OnButtonUp;
            CmdC.Up += OnButtonUp;
            CmdV.Up += OnButtonUp;
            CmdB.Up += OnButtonUp;
            CmdN.Up += OnButtonUp;
            CmdM.Up += OnButtonUp;
            CmdGreaterThan.Up += OnButtonUp;
            CmdLessThan.Up += OnButtonUp;
            CmdQuestion.Up += OnButtonUp;

            CmdSpaceBar.Up += OnButtonUp;
            CmdClear.Up += OnButtonUp;
            CmdClose.Up += OnButtonUp;

            _keyMap.Add(Key.D0, Cmd0);
            _buttonMap.Add(Cmd0, Key.D0);
            _keyMap.Add(Key.D1, Cmd1);
            _buttonMap.Add(Cmd1, Key.D1);
            _keyMap.Add(Key.D2, Cmd2);
            _buttonMap.Add(Cmd2, Key.D2);
            _keyMap.Add(Key.D3, Cmd3);
            _buttonMap.Add(Cmd3, Key.D3);
            _keyMap.Add(Key.D4, Cmd4);
            _buttonMap.Add(Cmd4, Key.D4);
            _keyMap.Add(Key.D5, Cmd5);
            _buttonMap.Add(Cmd5, Key.D5);
            _keyMap.Add(Key.D6, Cmd6);
            _buttonMap.Add(Cmd6, Key.D6);
            _keyMap.Add(Key.D7, Cmd7);
            _buttonMap.Add(Cmd7, Key.D7);
            _keyMap.Add(Key.D8, Cmd8);
            _buttonMap.Add(Cmd8, Key.D8);
            _keyMap.Add(Key.D9, Cmd9);
            _buttonMap.Add(Cmd9, Key.D9);
            _keyMap.Add(Key.A, CmdA);
            _buttonMap.Add(CmdA, Key.A);
            _keyMap.Add(Key.B, CmdB);
            _buttonMap.Add(CmdB, Key.B);
            _keyMap.Add(Key.C, CmdC);
            _buttonMap.Add(CmdC, Key.C);
            _keyMap.Add(Key.D, CmdD);
            _buttonMap.Add(CmdD, Key.D);
            _keyMap.Add(Key.E, CmdE);
            _buttonMap.Add(CmdE, Key.E);
            _keyMap.Add(Key.F, CmdF);
            _buttonMap.Add(CmdF, Key.F);
            _keyMap.Add(Key.G, CmdG);
            _buttonMap.Add(CmdG, Key.G);
            _keyMap.Add(Key.H, CmdH);
            _buttonMap.Add(CmdH, Key.H);
            _keyMap.Add(Key.I, CmdI);
            _buttonMap.Add(CmdI, Key.I);
            _keyMap.Add(Key.J, CmdJ);
            _buttonMap.Add(CmdJ, Key.J);
            _keyMap.Add(Key.K, CmdK);
            _buttonMap.Add(CmdK, Key.K);
            _keyMap.Add(Key.L, CmdL);
            _buttonMap.Add(CmdL, Key.L);
            _keyMap.Add(Key.M, CmdM);
            _buttonMap.Add(CmdM, Key.M);
            _keyMap.Add(Key.N, CmdN);
            _buttonMap.Add(CmdN, Key.N);
            _keyMap.Add(Key.O, CmdO);
            _buttonMap.Add(CmdO, Key.O);
            _keyMap.Add(Key.P, CmdP);
            _buttonMap.Add(CmdP, Key.P);
            _keyMap.Add(Key.Q, CmdQ);
            _buttonMap.Add(CmdQ, Key.Q);
            _keyMap.Add(Key.R, CmdR);
            _buttonMap.Add(CmdR, Key.R);
            _keyMap.Add(Key.S, CmdS);
            _buttonMap.Add(CmdS, Key.S);
            _keyMap.Add(Key.T, CmdT);
            _buttonMap.Add(CmdT, Key.T);
            _keyMap.Add(Key.U, CmdU);
            _buttonMap.Add(CmdU, Key.U);
            _keyMap.Add(Key.V, CmdV);
            _buttonMap.Add(CmdV, Key.V);
            _keyMap.Add(Key.W, CmdW);
            _buttonMap.Add(CmdW, Key.W);
            _keyMap.Add(Key.X, CmdX);
            _buttonMap.Add(CmdX, Key.X);
            _keyMap.Add(Key.Y, CmdY);
            _buttonMap.Add(CmdY, Key.Y);
            _keyMap.Add(Key.Z, CmdZ);
            _buttonMap.Add(CmdZ, Key.Z);
            _keyMap.Add(Key.CapsLock, CmdCapsLock);
            _buttonMap.Add(CmdCapsLock, Key.CapsLock);
            _keyMap.Add(Key.Enter, CmdEnter);
            _buttonMap.Add(CmdEnter, Key.Enter);
            _keyMap.Add(Key.Back, CmdBackspace);
            _buttonMap.Add(CmdBackspace, Key.Back);
            _keyMap.Add(Key.Space, CmdSpaceBar);
            _buttonMap.Add(CmdSpaceBar, Key.Space);
            _keyMap.Add(Key.Tab, CmdTab);
            _buttonMap.Add(CmdTab, Key.Tab);
            _keyMap.Add(Key.Clear, CmdClear);
            _buttonMap.Add(CmdClear, Key.Clear);
            _keyMap.Add(Key.Escape, CmdClose);
            _buttonMap.Add(CmdClose, Key.Escape);
            _keyMap.Add(Key.LeftShift, CmdShiftLeft);
            _buttonMap.Add(CmdShiftLeft, Key.LeftShift);
            _keyMap.Add(Key.RightShift, CmdShiftRight);
            _buttonMap.Add(CmdShiftRight, Key.RightShift);
            _keyMap.Add(Key.OemTilde, CmdTlide);
            _buttonMap.Add(CmdTlide, Key.OemTilde);
            _keyMap.Add(Key.OemMinus, CmdMinus);
            _buttonMap.Add(CmdMinus, Key.OemMinus);
            _keyMap.Add(Key.OemPlus, CmdPlus);
            _buttonMap.Add(CmdPlus, Key.OemPlus);
            _keyMap.Add(Key.OemOpenBrackets, CmdOpenCrulyBrace);
            _buttonMap.Add(CmdOpenCrulyBrace, Key.OemOpenBrackets);
            _keyMap.Add(Key.OemCloseBrackets, CmdEndCrultBrace);
            _buttonMap.Add(CmdEndCrultBrace, Key.OemCloseBrackets);
            _keyMap.Add(Key.OemBackslash, CmdOR);
            _buttonMap.Add(CmdOR, Key.OemBackslash);
            _keyMap.Add(Key.OemSemicolon, CmdColon);
            _buttonMap.Add(CmdColon, Key.OemSemicolon);
            _keyMap.Add(Key.OemQuotes, CmdDoubleInvertedComma);
            _buttonMap.Add(CmdDoubleInvertedComma, Key.OemQuotes);
            _keyMap.Add(Key.OemPeriod, CmdGreaterThan);
            _buttonMap.Add(CmdGreaterThan, Key.OemPeriod);
            _keyMap.Add(Key.OemComma, CmdLessThan);
            _buttonMap.Add(CmdLessThan, Key.OemComma);
            _keyMap.Add(Key.OemQuestion, CmdQuestion);
            _buttonMap.Add(CmdQuestion, Key.OemQuestion);

            foreach (Key key in _keyMap.Keys)
            {
                _keySatesMap.Add(key, KeyStates.None);
            }

        }

        private void OnButtonDown(Object sender, ButtonEventArgs args)
        {
            if (sender == this.CmdShiftLeft || sender == this.CmdShiftRight)
            {
                _modifiers = _modifiers | ModifierKeys.Shift;
            }

            UButton button = sender as UButton;
            if (button != null && _buttonMap.ContainsKey(button))
            {
                _keySatesMap[_buttonMap[button]] = _keySatesMap[_buttonMap[button]] | KeyStates.Down;

                if (KeyDown != null)
                    KeyDown(new KeyInputEventArgs(this, InputEventArgs.GetTimestamp(), _buttonMap[button]));
            }
        }

        private void OnButtonUp(Object sender, ButtonEventArgs args)
        {
            if (sender == this.CmdShiftLeft || sender == this.CmdShiftRight)
            {
                _modifiers = _modifiers | ~ModifierKeys.Shift;
            }

            UButton button = sender as UButton;
            if (button != null && _buttonMap.ContainsKey(button))
            {
                _keySatesMap[_buttonMap[button]] = _keySatesMap[_buttonMap[button]] & ~KeyStates.Down;

                if (KeyUp != null)
                    KeyUp(new KeyInputEventArgs(this, InputEventArgs.GetTimestamp(), _buttonMap[button]));
            }

        }

        private void OnButtonPressed(Object sender, ButtonEventArgs args)
        {
            UButton button = sender as UButton;

            if (button != null && _buttonMap.ContainsKey(button))
            {
                if (button == CmdCapsLock)
                {
                    if ((_keySatesMap[_buttonMap[button]] & KeyStates.Toggled) == KeyStates.Toggled)
                        _keySatesMap[_buttonMap[button]] = _keySatesMap[_buttonMap[button]] & ~KeyStates.Toggled;
                    else
                        _keySatesMap[_buttonMap[button]] = _keySatesMap[_buttonMap[button]] | KeyStates.Toggled;
                }
                if (KeyPressed != null)
                    KeyPressed(new KeyInputEventArgs(this, InputEventArgs.GetTimestamp(), _buttonMap[button]));
            }
        }

        #region IKeyboard Members

        public ModifierKeys Modifiers
        {
            get { return _modifiers; }
        }

        public IInputNode Target
        {
            get { return _target; }
        }

        public IInputNode Focus(IInputNode element)
        {
            _target = element;
            return _target;
        }

        public KeyStates GetKeyStates(Key key)
        {
            if (_keySatesMap.ContainsKey(key))
            {
                return _keySatesMap[key];
            }
            else
                return KeyStates.None;
        }
        
        public bool IsKeyDown(Key key)
        {
            return _keySatesMap.ContainsKey(key) && (_keySatesMap[key] & KeyStates.Down) == KeyStates.Down;
        }

        public bool IsKeyToggled(Key key)
        {
            return _keySatesMap.ContainsKey(key) && (_keySatesMap[key] & KeyStates.Toggled) == KeyStates.Toggled;
        }

        public bool IsKeyUp(Key key)
        {
            return !_keySatesMap.ContainsKey(key) || (_keySatesMap[key] & KeyStates.Down) == KeyStates.None;
        }

        public new event KeyInputEventHandler KeyDown;

        public new event KeyInputEventHandler KeyUp;

        public event KeyInputEventHandler KeyPressed;

        #endregion
    }
}
